package com.briup.estore.mapper;

import java.math.BigInteger;
import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.Update;

import com.briup.estore.bean.OrderForm;
import com.briup.estore.bean.OrderLine;

@Mapper
public interface IOrderMapper {

	@Select("select o.id order_id,o.cost order_cost,o.orderdate order_date,o.address_id order_address_id,"
			+ "o.customer_id order_customer_id, "
			+ "l.id line_id,l.num line_num,l.cost line_cost,l.book_id line_book_id,l.order_id line_order_id,"
			+ "b.id book_id,b.name book_name,b.price book_price,b.author book_author,"
			+ "b.publisher book_publisher,b.pubdate book_pubdate,b.description book_description,"
			+ "b.category_id book_catefory_id,b.click book_click from es_order o,es_line l ,es_book b"
			+ "where o.id = l.order_id and b.id = l.book_id and o.customer_id = #{customer} ")
	@ResultMap("com.briup.estore.mapper.IShopCarMapper.orderFormResultMap")
	List<OrderForm> findOrderFormWithOrderLineByCustomerId(BigInteger customer_id);

	@Insert("insert into es_order(id,cost,orderdate,address_id,customer_id) values(#{id},#{cost},#{orderdate},"
			+ "#{address_id},#{customer_id})")
	@SelectKey(statement = "select my_seq.nextval from dual ", keyProperty = "id", resultType = BigInteger.class, before = true)
	int newOrderForm(OrderForm orderForm);

	@Insert("insert into es_line(id,num,cost,book_id,order_id) values(#{id},#{num},#{cost},#{book_id},#{order_id})")
	@SelectKey(statement = "select my_seq.nextval from dual ", keyProperty = "id", resultType = BigInteger.class, before = true)
	int insertOrderLine(OrderLine line);

	int deleteOrderLine(@Param("line_ids") List<BigInteger> line_ids);

	@Update("update es_order set cost=#{cost} where id = #{id} ")
	int updateOrderCost(@Param("id") BigInteger id, @Param("cost") double cost);

	@Select("select o.id order_id,o.cost order_cost,o.orderdate order_orderdate,"
			+ "o.address_id order_address_id,o.customer_id order_customer_id,"
			+ "a.id address_id,a.receivename address_receivename,a.address address_address,"
			+ "a.phone address_phone,a.customer_id address_customer_id  from es_order o,es_address a "
			+ "where o.address_id = a.id and o.customer_id = #{customer_id} ")
	@ResultMap("com.briup.estore.mapper.IOrderMapper.orderFormResultMap")
	List<OrderForm> findOrdersByCustomerId(BigInteger customer_id);

	@Delete("delete from es_line where order_id = #{order_id} ")
	int deleteOrderLinesByOrderId(BigInteger order_id);

	@Delete("delete from es_order where id = #{id} ")
	int deleteOrderById(BigInteger id);

	@Select("select l.id line_id,l.num line_num,l.cost line_cost,l.book_id line_book_id, "
			+ "l.order_id line_order_id, " + "b.id book_id,b.name book_name,b.price book_price,b.author book_author, "
			+ "b.publisher book_publisher,b.pubdate book_pubdate,b.description book_description, "
			+ "b.category_id book_catefory_id,b.click book_click from es_line l ,es_book b "
			+ "where b.id = l.book_id and l.order_id = #{order_id} ")
	@ResultMap("com.briup.estore.mapper.IOrderMapper.orderLineResultMap")
	List<OrderLine> findOrderLinesByOrderId(BigInteger order_id);

}
